package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @Author: ycy
 * @CreateDate: 2020/5/13 17:35
 */
@Slf4j
public class IoDemo3 {

  public static void main(String[] args) {

    System.out.println(" ".getBytes(StandardCharsets.UTF_8));
    System.out.println(" ");

    // 获取桌面路径
    String userHome = System.getProperty("user.home");
    String desktopPath = Paths.get(userHome, "Desktop", "output3.txt").toString();

    long currentTimeMillis = System.currentTimeMillis();

    // 写入 10 万行数据
    int numberOfLines = 10000000;

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(desktopPath))) {
      for (int i = 1; i <= numberOfLines; i++) {
        String line = "Line " + i + System.currentTimeMillis() +" "  + System.lineSeparator();
        writer.write(line);
        writer.newLine();  // 写入系统的换行符
      }
      System.out.println("Data written to " + desktopPath);
      log.debug("cost-> {}" , System.currentTimeMillis() - currentTimeMillis);

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main2(String[] args) {


    // 获取桌面路径
    String userHome = System.getProperty("user.home");
    String desktopPath = Paths.get(userHome, "Desktop", "output2.txt").toString();

    long currentTimeMillis = System.currentTimeMillis();

    // 每行数据的长度大致为 10 个字节
    int numberOfLines = 10;
    int estimatedBytesPerLine = 30; // 估计每行的最大字节数
    long fileSize = numberOfLines * estimatedBytesPerLine;

    try (RandomAccessFile file = new RandomAccessFile(desktopPath, "rw");
         FileChannel fileChannel = file.getChannel()) {

      // 设置文件的大小
      file.setLength(fileSize);

      // 将文件映射到内存中
      MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);

      // 写入 10 万行数据
      for (int i = 1; i <= numberOfLines; i++) {
        String line = "Line " + i + System.currentTimeMillis() +" "  + System.lineSeparator();
        buffer.put(line.getBytes(StandardCharsets.UTF_8));
      }

      System.out.println("Data written to " + desktopPath);

     log.debug("cost-> {}" , System.currentTimeMillis() - currentTimeMillis);

    } catch (IOException e) {
      e.printStackTrace();
    }

    while(true){
      Scanner sco = new Scanner(System.in);
      System.out.println("your name");
      if("q".equalsIgnoreCase(sco.nextLine())){
        return;
      }
    }

  }


}
